package com.github.niefy.modules.third.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.modules.third.dao.ThirdTemplateMsgDetailMapper;
import com.github.niefy.modules.third.entity.ThirdTemplateMsgDetail;
import com.github.niefy.modules.third.entity.ThirdTemplateMsgDetailLog;
import com.github.niefy.modules.third.service.ThirdTemplateMsgDetailLogService;
import com.github.niefy.modules.third.service.ThirdTemplateMsgDetailService;
import com.github.niefy.modules.third.service.ThirdTemplateMsgUnreadService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuxy
 * @date 2023-4-4
 */
@Slf4j
@Service
public class ThirdTemplateMsgDetailServiceImpl extends ServiceImpl<ThirdTemplateMsgDetailMapper, ThirdTemplateMsgDetail> implements ThirdTemplateMsgDetailService {
    
	@Autowired
	private WxMpService wxMpService;
	@Autowired
	private ThirdTemplateMsgDetailLogService thirdTemplateMsgDetailLogService;
	@Autowired
	private ThirdTemplateMsgUnreadService thirdTemplateMsgUnreadService;

	@Override
	public String getMsgContent(Long msgId, String thirdId){
		var thirdTemplateMsgDetail = baseMapper.selectOne(lambdaQuery()
				.eq(ThirdTemplateMsgDetail::getMsgId, msgId)
				.eq(ThirdTemplateMsgDetail::getStudentNo, thirdId)
				.getWrapper()
		);
		//已查看的消息记录由third_template_msg_detail移至third_template_msg_detail_log,
		//同时由未读记录表移至已读记录表
		var thirdTemplateMsgDetailLog = BeanUtil.copyProperties(thirdTemplateMsgDetail, ThirdTemplateMsgDetailLog.class);
		baseMapper.deleteById(msgId);
		thirdTemplateMsgDetailLogService.save(thirdTemplateMsgDetailLog);
		thirdTemplateMsgUnreadService.unreadMsgToreadMsg(msgId);
		return thirdTemplateMsgDetail.getMsgContent();
	}
}
